<?php

namespace YMApi\Model;

use Think\Model;

class FavourableActivityModel extends Model {
	/**
	 *
	 * @param $user_rank 用户等级        	
	 * @param $page 当前页数        	
	 * @return $data 优惠活动数据
	 */
	public function get_activity_list($user_rank, $page) {
		$map = array ();
		$map ["_string"] = "FIND_IN_SET('$user_rank', user_rank)";
		
		$count = $this->get_activity_count ( $map );
		
		$pagearr = show_page ( $count, $page, C ( 'page_size' ) );
		
		$res = $this->field ( 'act_id,act_name,start_time,end_time,user_rank,act_range,act_range_ext,min_amount,max_amount,act_type,act_type_ext,gift,sort_order,touch_img' )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->select ();
		$data = array ();
		if ($res) {
			$data ['list'] = $res;
		}
		if ($page >= $pagearr ['pagecount']) {
			$data ['is_bottom'] = 1;
		} else {
			$data ['is_bottom'] = 0;
		}
		return $data;
	}
	/**
	 * 
	 * @param $act_id 优惠活动id 
	 * @param $page 页数  
	 * @return $data 返回优惠活动相关商品数据
	 */
	public function get_activity_goods($act_id, $page,$wid=0) {
		$map = array ();
		$map ['act_id'] = $act_id;
		$res = $this->field ( 'act_range,act_range_ext,act_type,act_type_ext,quota' )->where ( $map )->find ();
		if ($res) {
// 		    if($wid){
// 		        $goods = M('goods');
// 		        $map = array (
// 		            'g.is_on_sale' => 1,
// 		            'g.is_delete' => 0,
// 		        	'gs.wid'=>$wid
// 		        );
// 		    }else{
// 		        $goods = M ('goods');
// 		        $map = array (
// 		            'is_on_sale' => 1,
// 		            'is_delete' => 0
// 		        );
// 		    }
			
		    $goods = M ('goods');
		    $map = array (
		    		'is_on_sale' => 1,
		    		'is_delete' => 0
		    );
		    
			if ($res ['act_range'] != FAR_ALL && ! empty ( $res ['act_range_ext'] )) {
				// 不是全部商品为优惠活动
				$act_range_ext = explode ( ',', $res ['act_range_ext'] );
// 				if($wid){
// 				    $field = $this->convert_goods_filed ( $res ['act_type'], $res ['act_type_ext'],$wid);
// 				    //临时修改缩略图，取原图
// 				    $field = str_replace('g.goods_thumb', 'g.goods_img as goods_thumb', $field);
// 				}else{
// 				    $field = $this->convert_goods_filed ( $res ['act_type'], $res ['act_type_ext']);
// 				    //临时修改缩略图，取原图
// 				    $field = str_replace('goods_thumb', 'goods_img as goods_thumb', $field);
// 				}
				
				$field = $this->convert_goods_filed ( $res ['act_type'], $res ['act_type_ext']);
				//临时修改缩略图，取原图
				$field = str_replace('goods_thumb', 'goods_img as goods_thumb', $field);
				if ($res ['act_range'] == FAR_CATEGORY) {
					// 按分类
					// 从商品扩展分类表获取相关的商品id
					$goods_cat = D ( 'goods_cat' );
					$goods_cat_gid = $goods_cat->where ( array (
							'cate_id' => array (
									'in',
									$act_range_ext 
							) 
					) )->getField ( 'goods_id', true );
					// 获取相关优惠商品
// 					if($wid){
//     					$where ['g.cat_id'] = array (
//     							'in',
//     							$act_range_ext 
//     					);
//     					$where ['g.goods_id'] = array (
//     							'in',
//     							$goods_cat_gid 
//     					);
// 					}else{
// 					    $where ['cat_id'] = array (
// 					        'in',
// 					        $act_range_ext
// 					    );
// 					    $where ['goods_id'] = array (
// 					        'in',
// 					        $goods_cat_gid
// 					    );
// 					}
					$where ['cat_id'] = array (
							'in',
							$act_range_ext
					);
					$where ['goods_id'] = array (
							'in',
							$goods_cat_gid
					);
					$where ['_logic'] = 'or';
					$map ['_complex'] = $where;
// 					if($wid){
// 					    $count = $goods->alias('g')->join(C ( 'DB_PREFIX' ) . 'goods_storage as gs on gs.goods_id=g.goods_id')->where ( $map )->count ();
// 					}else{
// 					    $count = $goods->where ( $map )->count ();
// 					}
					$count = $goods->where ( $map )->count ();
					$pagearr = show_page ( $count, $page, C ( 'page_size' ) );
// 					if($wid){
// 					    $goodslist = $goods->alias('g')->join(C ( 'DB_PREFIX' ) . 'goods_storage as gs on gs.goods_id=g.goods_id')->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'g.sort_order asc' )->select ();
// 					}else{
// 					    $goodslist = $goods->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'sort_order asc' )->select ();
// 					}
					$goodslist = $goods->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'sort_order asc' )->select ();
				} elseif ($res ['act_range'] == FAR_BRAND) {
					// 按品牌
// 				    if($wid){
//     				    $map ['g.brand_id'] = array (
//     							'in',
//     							$act_range_ext 
//     					);
// 				    }else{
// 				        $map ['brand_id'] = array (
// 				            'in',
// 				            $act_range_ext
// 				        );
// 				    }
// 				    if($wid){
// 				        $count = $goods->alias('g')->join(C ( 'DB_PREFIX' ) . 'goods_storage as gs on gs.goods_id=g.goods_id')->where ( $map )->count ();
// 				    }else{
// 				        $count = $goods->where ( $map )->count ();
// 				    }
				    $map ['brand_id'] = array (
				    		'in',
				    		$act_range_ext
				    );
				    $count = $goods->where ( $map )->count ();
					$pagearr = show_page ( $count, $page, C ( 'page_size' ) );
// 					if($wid){
// 					    $goodslist = $goods->alias('g')->join(C ( 'DB_PREFIX' ) . 'goods_storage as gs on gs.goods_id=g.goods_id')->where ( $map )->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'g.sort_order asc' )->select ();
// 					}else{
// 					    $goodslist = $goods->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'sort_order asc' )->select ();
// 					}
					$goodslist = $goods->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'sort_order asc' )->select ();
				} else {
					// 按商品
// 				    if($wid){
//     				    $map ['g.goods_id'] = array (
//     							'in',
//     							$act_range_ext 
//     					);
//     				    $count = $goods->alias('g')->join(C ( 'DB_PREFIX' ) . 'goods_storage as gs on gs.goods_id=g.goods_id')->where ( $map )->count ();
// 				    }else{
// 				        $map ['goods_id'] = array (
// 				            'in',
// 				            $act_range_ext
// 				        );
// 				        $count = $goods->where ( $map )->count ();
// 				    }
					$map ['goods_id'] = array (
							'in',
							$act_range_ext
					);
					$count = $goods->where ( $map )->count ();
					$pagearr = show_page ( $count, $page, C ( 'page_size' ) );
// 					if($wid){
// 					    $goodslist = $goods->alias('g')->join(C ( 'DB_PREFIX' ) . 'goods_storage as gs on gs.goods_id=g.goods_id')->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'g.sort_order asc' )->select ();
// 					}else{
// 					    $goodslist = $goods->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'sort_order asc' )->select ();
// 					}
					$goodslist = $goods->field ( $field )->where ( $map )->limit ( $pagearr ['firstpage'], $pagearr ['pagesize'] )->order ( 'sort_order asc' )->select ();
					if ($res ['act_type'] == FAT_LIMITBUY) {
						$act_goodslist = unserialize ( $res ['quota'] );
						foreach ( $goodslist as $key => $goods ) {
							foreach ( $act_goodslist as $act_goods ) {
								if ($goodslist [$key] ['goods_id'] == $act_goods ['id']) {
									$goodslist [$key] ['promote_price'] = $act_goods ['price'];
									$goodslist [$key] ['count'] = $act_goods ['count'];
								}
							}
						}
					}
				}
			}
			if ($goodslist) {
				$goodslist = $this->format_goodlist($goodslist,$wid);
				if ($page >= $pagearr ['pagecount']) {
					$data ['is_bottom'] = 1;
				} else {
					$data ['is_bottom'] = 0;
				}
				$data ['list'] = $goodslist;
			}
		}
		
		return $data;
	}
	private function get_activity_count($map) {
		return $this->where ( $map )->count ();
	}
	private function convert_goods_filed($act_type, $act_type_ext,$wid=0) {
		if($wid){
		    switch ($act_type) {
		        case '0' :
		            $field = "g.goods_id,g.goods_name,g.goods_number,g.goods_brief,g.goods_desc,g.goods_thumb,g.goods_img,g.shop_price,g.market_price,g.promote_price,$act_type as act_type,0 as act_promote_price,0 as  act_promote_price_rate,g.virtual_sales,g.goods_weight,g.spec_value,g.spec_unit,g.is_presell,g.delivery_type,g.delivery_cycle";
		            break;
		        case '1' :
		            $field = "g.goods_id,g.goods_name,g.goods_number,g.goods_brief,g.goods_desc,g.goods_thumb,g.goods_img,g.shop_price,g.market_price,g.shop_price,g.promote_price,$act_type as act_type,$act_type_ext as act_promote_price,0 as act_promote_price_rate,g.virtual_sales,g.goods_weight,g.spec_value,g.spec_unit,g.is_presell,g.delivery_type,g.delivery_cycle";
		            break;
		        case '2' :
		            $field = "g.goods_id,g.goods_name,g.goods_number,g.goods_brief,g.goods_desc,g.goods_thumb,g.goods_img,g.shop_price,g.market_price,g.promote_price,$act_type as act_type,0 as act_promote_price,$act_type_ext/100 as act_promote_price_rate,g.virtual_sales,g.goods_weight,g.spec_value,g.spec_unit,g.is_presell,g.delivery_type,g.delivery_cycle";
		            break;
		        default :
		            $field = "g.goods_id,g.goods_name,g.goods_number,g.goods_brief,g.goods_desc,g.goods_thumb,g.goods_img,g.shop_price, g.market_price,g.promote_price,$act_type as act_type,0 as act_promote_price,0 as  act_promote_price_rate,g.virtual_sales,g.goods_weight,g.spec_value,g.spec_unit,g.is_presell,g.delivery_type,g.delivery_cycle";
		            break;
		    }
		}else{
		    switch ($act_type) {
		        case '0' :
		            $field = "goods_id,goods_name,goods_number,goods_brief,goods_desc,goods_thumb,goods_img,shop_price, market_price,promote_price,$act_type as act_type,0 as act_promote_price,0 as  act_promote_price_rate,virtual_sales,goods_weight,spec_value,spec_unit,is_presell,delivery_type,delivery_cycle";
		            break;
		        case '1' :
		            $field = "goods_id,goods_name,goods_number,goods_brief,goods_desc,goods_thumb,goods_img,shop_price, market_price,shop_price,promote_price,$act_type as act_type,$act_type_ext as act_promote_price,0 as act_promote_price_rate,virtual_sales,goods_weight,spec_value,spec_unit,is_presell,delivery_type,delivery_cycle";
		            break;
		        case '2' :
		            $field = "goods_id,goods_name,goods_number,goods_brief,goods_desc,goods_thumb,goods_img,shop_price, market_price,promote_price,$act_type as act_type,0 as act_promote_price,$act_type_ext/100 as act_promote_price_rate,virtual_sales,goods_weight,spec_value,spec_unit,is_presell,delivery_type,delivery_cycle";
		            break;
		        default :
		            $field = "goods_id,goods_name,goods_number,goods_brief,goods_desc,goods_thumb,goods_img,shop_price, market_price,promote_price,$act_type as act_type,0 as act_promote_price,0 as  act_promote_price_rate,virtual_sales,goods_weight,spec_value,spec_unit,is_presell,delivery_type,delivery_cycle";
		            break;
		    }
		}
		return $field;
	}
	private function format_goodlist($goodslist,$wid=0) {
		$list = array ();
		foreach ($goodslist as $key=>$val){
			$data = array ();
			$data ['goods_id'] = $val ['goods_id'];
			$data ['goods_name'] = $val ['goods_name'];
			$data['shop_price'] = $val['shop_price'];
			$data['market_price'] = $val['market_price'];
			$data['goods_number'] = $val['goods_number'];
			$data['goods_brief'] = $val['goods_brief'];
			$data['goods_desc'] = $val['goods_desc'];
			$data['goods_thumb'] = img_init($val['goods_thumb']);
			$data['goods_img'] = img_init($val['goods_img']);
			$data['goods_weight'] = $val['goods_weight'];
			if($val['spec_value']){
				$data['spec_value'] = $val ['spec_value'];
				$data['spec_unit'] = $val ['spec_unit'];
			}elseif($val['goods_weight']){
				$data['spec_value'] =  $val['goods_weight']*1000;
				$data['spec_unit'] =  "g";
			}
			if ($val ['act_type'] == FAT_PRICE) {
				$data ['promote_price'] = $val ['promote_price'] ? ($val ['promote_price'] - $val ['act_promote_price']) : $val ['shop_price'] - $val ['act_promote_price'];
			} elseif ($val ['act_type'] == FAT_DISCOUNT) {
				$data ['promote_price'] = $val ['promote_price'] ? ($val ['promote_price'] * $val ['act_promote_price_rate']) : ($val ['shop_price'] * $val ['act_promote_price_rate']);
			}elseif ($val ['act_type'] == FAT_LIMITBUY){
				$data ['promote_price'] = $val ['promote_price'];
			}
			if($data ['promote_price'] <0){
				$data ['promote_price']=0;
			}
			
			if($val ['count']){
				$data ['count'] = $val ['count'];
			}else{
				$data ['count'] = 0;
			}
			$data['sales_count'] = D('goods')->get_goods_sales_count($val ['goods_id']);
			$data['sales_count'] += $val['virtual_sales'];
			$val['is_presell']=(int)$val['is_presell'];
			if($val['is_presell']!=0){
    			//检查是否该商品是否是预售商品,若是预售商品，则返回1,否则是现售返回0;
    			$val['is_presell']=D('Goodsstorage')->check_whether_presell($val['goods_id'],$wid);
			}
			$data['is_presell']=$val['is_presell'];
			if($val['is_presell']){
			    if($val['delivery_type']==1){  //分段发货
			        $Goodspreselltime=D('Goodspreselltime');
			        if($Goodspreselltime->check_shipping_goodspreselltime($val['goods_id'])){//检查预售商品是否设置发货配送时间
			            $preselltimeres=$Goodspreselltime->check_shippingtime($val['goods_id'],time());
			            if($preselltimeres){
			                $data['preselltimeres']=array('delivery_time'=>$preselltimeres['delivery_time']);
			            }else{
			                $data['preselltimeres']=array('delivery_time'=>'');
			            }
			        }else{
			            $data['preselltimeres']=array('delivery_time'=>'');
			        } 
			    }elseif($val['delivery_type']==2){ //定时方式
			        if($val['delivery_cycle']){
			            $gmd=local_date('Y-m-d',gmtime());
			            $goods_presell_time = local_date('Y-m-d H:i:s', local_strtotime($gmd.' +'.$val['delivery_cycle'].' day '.C('DEFAULT_PRESELL_TIME_H')));
			            $data['preselltimeres']=array('delivery_time'=>$goods_presell_time);
			        }else{
			            $data['preselltimeres']=array('delivery_time'=>'');
			        }
			    }else{
			        $data['preselltimeres']=array('delivery_time'=>'');
			    }
			}else{
			    $goodsstorage=D('Goodsstorage')->get_warehous_goods($wid,$val['goods_id']);
			    if($goodsstorage){
			        $data['goods_number']=$goodsstorage['practical_num'];
			    }else{
			        $data['goods_number']=0;
			    }
			    $data['preselltimeres']=array('delivery_time'=>'');
			}
			$list[] = $data;
		}
		return $list;
	}
	
	public function favourable_info($act_id){
		$map = array('act_id'=>$act_id);
		
		$row = $this->where($map)->find();
		if (!empty($row)) {
			$row['_start_time'] = $row['start_time'];
			$row['_end_time'] = $row['end_time'];
			$row['start_time'] = local_date(C('time_format'), $row['start_time']);
			$row['end_time'] = local_date(C('time_format'), $row['end_time']);
			$row['formated_min_amount'] = price_format($row['min_amount']);
			$row['formated_max_amount'] = price_format($row['max_amount']);
			$row['gift'] = unserialize($row['gift']);
			$row['quota'] = unserialize($row['quota']);
			if ($row['act_type'] == FAT_GOODS) {
				$row['act_type_ext'] = round($row['act_type_ext']);
			}
		}
		return $row;
	}
	

    /**
     * 判断当前商品是否享受折扣价格
     * Created by Jerry
     * @param $uid
     * @param $goods_id
     * @return bool
     */
	public function get_is_discount($uid, $goods_id)
    {
        //        获取当前有效的折扣数据
        $discountData = $this->favourable_discout_list_flow($uid);

        foreach ($discountData as $k=>$v) {
            if($v['goods_id'] == $goods_id){
                return true;
            }
        }

        return false;
    }

    /**
     * 判断指定商品是否超过折扣限制数量
     * Created by Jerry
     * @param $uid
     * @param $goods_id
     * @param $goods_number
     * @return bool
     */
    public function get_discount_limited_number_by_id($uid, $goods_id, $goods_number)
    {
        //        获取当前有效的折扣数据
        $discountData = $this->favourable_discout_list_flow($uid);

        foreach ($discountData as $k=>$v) {
            if($v['goods_id'] != $goods_id){
                continue;
            }

//            检查是否在限购数量范围
            if($v['limited_number'] > 0){
                if($goods_number > $v['limited_number']){
                    return false;
                }
            }

        }

        return true;

    }


    /**
     * 检查当前指定商品是否有折扣价格并返回折扣后的商品价格
     * Created by Jerry
     * @param int $user_id
     * @param int $goods_id
     * @param int $goods_number
     * @return array|bool
     */
    public function check_discount_by_goods_id_set_price($user_id = 0, $goods_id = 0, $goods_number = 0)
    {
//        获取当前有效的折扣数据
        $discountData = $this->favourable_discout_list_flow($user_id);

        //获取用户等级
        $user_rank = D('users')->where(array('user_id'=>$user_id))->getField('user_rank');

//        获取商品原价
        $goods_price = $this->get_goods_by_id_for_price($goods_id);
//        获取商品最终价格
        $find_goods_price = $this->get_goods_by_id_for_find_price($goods_id, $user_id);

        $goodsDiscountData = array();

//        享受折扣的商品数量
        $number_price = 1;
        foreach ($discountData as $k=>$v) {
            if($v['goods_id'] != $goods_id){
                continue;
            }

//            检测是否在限购数量范围
            if($v['limited_number'] > 0){
                if($goods_number > $v['limited_number']){
                    return false;
                }
            }


            /**
             * 判断是否在限制数量内满多少件折扣生效
             */
            if($v['limited_discount'] > 0){
                $number_price = intval($goods_number / $v['limited_discount']);
            }

//            插入id
            $goodsDiscountData['act_id'] = $v['act_id'];

//            判断是否折扣价
            if($v['discount_price'] > 0){

//                if($user_rank == 2){
//                    $benefit_price = $goods_price * ($v['discount_price'] / 10);
////                    dd($find_goods_price - $benefit_price);
////                  如果会员价比商品加便宜则不使用折扣
//                    if($find_goods_price < $benefit_price){
//                        continue;
//                    }
//
//                    $benefit_price = $find_goods_price - $benefit_price;
//                    $goodsDiscountData['benefit_price'] = $benefit_price * $number_price;
//                    $goodsDiscountData['down_price'] = $v['down_price'];
//                }else{
//                    /**
//                     * 计算原价与折扣价差价，乘以数量得出全部数量优惠价
//                     */
//                    $benefit_price = $goods_price * ($v['discount_price'] / 10);
//                    $goodsDiscountData['benefit_price'] = ($goods_price - $benefit_price) * $number_price;
//                    $goodsDiscountData['discount_price'] = $v['discount_price'] + 0;
//                }

                /**
                 * 计算原价与折扣价差价，乘以数量得出全部数量优惠价
                 */
                $benefit_price = $goods_price * ($v['discount_price'] / 10);
                $goodsDiscountData['benefit_price'] = ($goods_price - $benefit_price) * $number_price;
                $goodsDiscountData['discount_price'] = $v['discount_price'] + 0;
            }

//            判断是否减价
            if($v['down_price'] > 0){

//                if($user_rank == 2){
//                    $price = $goods_price - $v['down_price'];
////                  如果会员价比商品加便宜则不使用满减
//                    if($find_goods_price < $price){
//                        continue;
//                    }
//
//                    $benefit_price = $find_goods_price - $price;
//                    $goodsDiscountData['benefit_price'] = $benefit_price * $number_price;
//                    $goodsDiscountData['down_price'] = $v['down_price'];
//                }else{
//                    /**
//                     * 计算原价与减价差价，乘以数量得出全部数量优惠价
//                     */
//                    $benefit_price = $goods_price - ($goods_price - $v['down_price']);
//                    $goodsDiscountData['benefit_price'] = $benefit_price * $number_price;
//                    $goodsDiscountData['down_price'] = $v['down_price'];
//                }
                /**
                 * 计算原价与减价差价，乘以数量得出全部数量优惠价
                 */
                $benefit_price = $goods_price - ($goods_price - $v['down_price']);
                $goodsDiscountData['benefit_price'] = $benefit_price * $number_price;
                $goodsDiscountData['down_price'] = $v['down_price'];
            }


        }

        return $goodsDiscountData;

    }

    /**
     * 获取指定商品的商品价格
     * Created by Jerry
     * @param $goods_id
     * @return mixed
     */
    protected function get_goods_by_id_for_price($goods_id)
    {
        $goodsModel = D('Goods');
        return $goodsModel->get_goods_price_for_discount_by_id($goods_id);
    }

    /**
     * 获取商品最终价格
     * Created by Jerry
     * @param $goods_id
     * @param $user_id
     * @return mixed
     */
    protected function get_goods_by_id_for_find_price($goods_id, $user_id)
    {
        $goodsModel = D('Goods');
        return $goodsModel->get_final_price($goods_id, 1, false, array(), $user_id);
    }


    /**
     * 返回指定用户享受的优惠折扣
     * Created by Jerry
     * @param int $uid
     * @return array|bool
     */
    public function favourable_discout_list_flow($uid = 0)
    {
        //获取用户等级
        $user_rank = D('users')->where(array('user_id'=>$uid))->getField('user_rank');

        if(empty($user_rank)){
            $user_rank['user_rank'] = 0;
        }

        $now = gmtime();

        $where = array(
            'start_time'    =>  array('ELT', $now),
            'end_time'      =>  array('EGT', $now),
            'act_type'      =>  FAT_DISCOUNT_LIMIT,
        );

        $favourable_activity_list = $this->where($where)->select();
        if(empty($favourable_activity_list)){
            return false;
        }

//        折扣减价
        $discountData = array();

        foreach ($favourable_activity_list as $key=>$favourable) {

//            时间校正
            $favourable['start_time'] = localtotime($favourable['start_time']);
            $favourable['end_time'] = localtotime($favourable['end_time']);

//            判断是否开始
            if($favourable['start_time'] > gmtime()){
                continue;
            }

//            判断是否过期
            if($favourable['end_time'] < gmtime()){
                continue;
            }

            /**
             * 会员等级是否匹配
             */
            $rank_id = strpos($favourable['user_rank'], $user_rank['user_rank']);
            if($rank_id === false){
                continue;
            }

            $discount = json_decode($favourable['discount'], true);

//            处理折扣数据结构
            if (!empty($discount)) {
                foreach ($discount as $k=>$v) {
                    $discountData[] = array(
                        'act_id'            =>  $favourable['act_id'],
                        'goods_id'          =>  $k,
                        'discount_price'    =>  $v['discount_price'],   //折扣率
                        'down_price'        =>  $v['down_price'],
                        'over_price'        =>  $v['over_price'],
                        'limited_number'    =>  $favourable['limited_number'],   //限制多少件内优惠
                        'limited_discount'  =>  $favourable['limited_discount'], //没多少件可打折
                    );
                }
            }
        }

        if(empty($discountData)){
            return false;
        }

        return $discountData;
    }
}
?>